package conference;


/**
 * 
 * @author Matt Minch
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import papers.Manuscript;
import papers.Review;

import users.User;


public class Conference implements Serializable{
	
	//For serialization
	private static final long serialVersionUID = -5794461782809822403L;
	
	private User my_programChair;
	private String my_conferenceName;
	private Date my_manuDeadline;
	private Date my_revDeadline;
	private List<Manuscript> my_manuscripts;
	private List<Review> my_reviews;
	private List<User> my_users;
	private boolean my_view_status;
	
	/**
	 * Creates a Conference Object.
	 * 
	 * @param newChair the program chair of the conference.
	 * @param name the name of the conference
	 * @param manuscript the date that a conference will stop accepting manuscripts. Submission of Manuscripts
	 * 					 after this date will not be possible.
	 * @param review the date that reviews are to be turned in.
	 * @param newUsers a List of User objects. These are the Users involved with this conference.
	 */
	public Conference(User newChair, String name, Date manuscript, Date review, List<User> newUsers)
	{
		my_programChair = newChair;
		my_conferenceName = name;
		my_manuDeadline = new Date();
		my_manuDeadline = manuscript;
		my_revDeadline = new Date();
		my_revDeadline = review;
		my_view_status = false;
				
		if (newUsers.contains(newChair))
		{
			my_users = newUsers;
		}
		else 
		{
			my_users = newUsers;
			my_users.add(newChair);
		}
				
		my_manuscripts = new ArrayList<Manuscript>();
		my_reviews = new ArrayList<Review>();
	}
	
	public void setViewStatus(final boolean the_status){
		my_view_status = the_status;
	}
	
	public boolean getViewStatus(){
		return my_view_status;
	}
		
	/**
	 * Allows Authors to submit Manuscript objects to the conference.
	 * 
	 * @param newManu the Manuscript object to be added to the conference.
	 * @return true if the conference adds the new Manuscript, false otherwise.
	 */
	public boolean submitManuscript(Manuscript newManu)
	{
//		if (!my_users.contains(newManu.getAuthor())) {
//			throw new IllegalArgumentException();
//		}
		my_manuscripts.add(newManu);
		newManu.getAuthor().addManuscript(newManu);
		if (my_manuscripts.contains(newManu))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Allows reviewers to submit Review objects to the conference.
	 * 
	 * @param newRev the Review being added to the conference.
	 * @return true if the Review is in the conference, false otherwise.
	 */
	 public boolean submitReview(Review newRev, Manuscript manu)
	    {
	        my_reviews.add(newRev);
	        manu.addReview(newRev);
	        manu.addReviewer(newRev.getUser());

		if(my_reviews.contains(newRev))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}

	/**
	 * Gets the List of Manuscripts and returns it.
	 * 
	 * @return manuscripts, the List of Manuscript objects in the conference.
	 */
	public List<Manuscript> getManuscripts()
	{
		return my_manuscripts;		
	}
	
	/**
	 * Gets the List of Reviews and returns it.
	 * 
	 * @return reviews, the List of Review objects in the conference.
	 */
	public List<Review> getReviews()
	{
		return my_reviews;		
	}
	
	/**
	 * Sets the deadlines for manuscripts.
	 * 
	 * @param newDeadline the new deadline for Manuscripts to be submitted.
	 * @return true if the deadline has been changed to the new deadline, false otherwise.
	 */
	public boolean setManuDate(Date newDeadline)
	{
		my_manuDeadline = newDeadline;
		if(my_manuDeadline.equals(newDeadline))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Gets the manuscript deadline for a conference.
	 * 
	 * @return manuscript_deadline, the Date by which manuscripts are due for a conference.
	 */
	public Date getManuDate()
	{
		return my_manuDeadline;
	}
		
	/**
	 * Sets the deadlines for reviews.
	 * 
	 * @param newDeadline the new deadline by which reviews are to be in.
	 * @return true if the deadline has been changed to the new date, false otherwise.
	 */
	public boolean setRevDate(Date newDeadline)
	{
		my_revDeadline = newDeadline;
		if(my_revDeadline.equals(newDeadline))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Gets the review deadline for a conference.
	 * 
	 * @return review_deadline, the Date by which reviews are due.
	 */
	public Date getRevDate()
	{
		return my_revDeadline;
	}
	
	/**
	 * Gets the program chair of a conference.
	 * 
	 * @return program_chair, the program chair of the conference.
	 */
	public User getPC()
	{
		return my_programChair;
	}
	
	/**
	 * Gets the conference name.
	 * 
	 * @return conference_name, the name of the conference.
	 */
	public String getConfName()
	{
		return my_conferenceName;
	}

	/**
	 * Gets the users involved in a conference.
	 * 
	 * @return users, the List of User objects in a conference.
	 */
	public List<User> getUsers()
	{
		return my_users;
	}
	
	public boolean addUser(User user) throws IOException{
		my_users.add(user);
		boolean isok = false;
		try {
			TextParse tp = new TextParse(new File(System.getProperty("user.dir") + File.separator + "config.txt"));
			isok = tp.addToFile(user, this);
		} catch (FileNotFoundException e) {
		}
		return isok;		
	}
	public void addUserNoCheck(User user) throws IOException{
		my_users.add(user);
		try {
			TextParse tp = new TextParse(new File(System.getProperty("user.dir") + File.separator + "config.txt"));
			tp.addToFileNoCheck(user, this);
		} catch (FileNotFoundException e) {
		}
	}
	
	/**
	 * for testing purposes.
	 */
	public String toString() {
		return my_programChair + "\t" + my_conferenceName + "\t" + my_manuDeadline +
				"\t"	+ my_manuscripts + "\t" + my_revDeadline + "\t" + my_reviews + "\t" + my_users;
	}
	
	/**
	 * Removes a specified manuscript from the manuscript list.
	 * @param paper the manuscript to be deleted
	 */
	public void delete(Manuscript paper){
		my_manuscripts.remove(paper);
	}
	
	
}
